<!DOCTYPE html>
<!-- saved from url=(0041)http://c.biancheng.net/view/vip_7084.html -->
<html lang="zh-CN"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script type="text/javascript" async="" charset="utf-8" src="./14.9Java通过反射获取注解信息_files/core.php"></script>

<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
<!-- 启用Chromium高速渲染模式 -->
<meta name="renderer" content="webkit">
<meta name="force-rendering" content="webkit">
<!-- 禁止百度转码 -->
<meta name="applicable-device" content="pc,mobile">
<meta name="MobileOptimized" content="width">
<meta name="HandheldFriendly" content="true">
<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
<!-- 禁止识别电话号码 -->
<meta name="format-detection" content="telephone=no">

<link rel="shortcut icon" href="http://c.biancheng.net/favicon.ico?v=1.6.72">
<link href="./14.9Java通过反射获取注解信息_files/common.css" rel="stylesheet">
<title>Java通过反射获取注解信息</title>
<meta name="description" content="使用注解修饰了类、方法、变量等成员之后，这些注解不会自己生效，必须由开发者提供相应的工具来提取处理。要想获取类、方法或变量的注解信息，必须通过 Java 的反射技术来获取">
</head>
<body>
<div id="topbar" class="clearfix">
	<ul id="product-type" class="left">
		<li>
			<a href="http://c.biancheng.net/"><span class="iconfont iconfont-home"></span>首页</a>
		</li>
		<li class="active">
			<a href="http://c.biancheng.net/sitemap/" rel="nofollow"><span class="iconfont iconfont-book"></span>教程</a>
		</li>
		<li>
			<a href="http://vip.biancheng.net/p/vip/show.php" rel="nofollow" target="_blank"><span class="iconfont iconfont-vip"></span>VIP会员</a>
		</li>
		<li>
			<a href="http://vip.biancheng.net/p/q2a/show.php" rel="nofollow" target="_blank"><span class="iconfont iconfont-q2a"></span>一对一答疑</a>
		</li>
		<li>
			<a href="http://fudao.biancheng.net/" rel="nofollow" target="_blank"><span class="iconfont iconfont-fudao"></span>辅导班</a>
		</li>
	</ul>
</div>
<div id="header" class="clearfix">
	<a id="logo" class="left" href="http://c.biancheng.net/">
		<img height="26" src="./14.9Java通过反射获取注解信息_files/logo.png" alt="C语言中文网">
	</a>
	<ul id="nav-main" class="hover-none left clearfix">
		<li class="wap-yes"><a href="http://c.biancheng.net/">首页</a></li>
		<li><a href="http://c.biancheng.net/c/">C语言教程</a></li>
		<li><a href="http://c.biancheng.net/cplus/">C++教程</a></li>
		<li><a href="http://c.biancheng.net/python/">Python教程</a></li>
		<li><a href="http://c.biancheng.net/java/">Java教程</a></li>
		<li><a href="http://c.biancheng.net/linux_tutorial/">Linux入门</a></li>
		<li><a href="http://c.biancheng.net/sitemap/" title="网站地图">更多&gt;&gt;</a></li>
	</ul>
	<span id="sidebar-toggle" class="toggle-btn" toggle-target="#sidebar">目录 <span class="glyphicon"></span></span>

	<a href="http://vip.biancheng.net/?from=topbar" class="user-info glyphicon glyphicon-user hover-none" target="_blank" rel="nofollow" title="用户中心"></a>
</div>
<div id="main" class="clearfix">
	<div id="sidebar" class="toggle-target">
	<div id="contents">
		<dt><span class="glyphicon glyphicon-option-vertical" aria-hidden="true"></span><a href="http://c.biancheng.net/java/">Java教程</a></dt>
		
			<dd>
				<span class="channel-num">1</span>
				<a href="http://c.biancheng.net/java/10/">Java入门基础及环境搭建</a>
			</dd>
		
			<dd>
				<span class="channel-num">2</span>
				<a href="http://c.biancheng.net/java/20/">Java程序设计基础</a>
			</dd>
		
			<dd>
				<span class="channel-num">3</span>
				<a href="http://c.biancheng.net/java/30/">Java流程控制语句</a>
			</dd>
		
			<dd>
				<span class="channel-num">4</span>
				<a href="http://c.biancheng.net/java/40/">Java字符串处理</a>
			</dd>
		
			<dd>
				<span class="channel-num">5</span>
				<a href="http://c.biancheng.net/java/50/">Java数字和日期处理</a>
			</dd>
		
			<dd>
				<span class="channel-num">6</span>
				<a href="http://c.biancheng.net/java/60/">Java内置包装类</a>
			</dd>
		
			<dd>
				<span class="channel-num">7</span>
				<a href="http://c.biancheng.net/java/70/">Java数组处理</a>
			</dd>
		
			<dd>
				<span class="channel-num">8</span>
				<a href="http://c.biancheng.net/java/80/">Java类和对象</a>
			</dd>
		
			<dd>
				<span class="channel-num">9</span>
				<a href="http://c.biancheng.net/java/90/">Java继承和多态</a>
			</dd>
		
			<dd>
				<span class="channel-num">10</span>
				<a href="http://c.biancheng.net/java/100/">Java异常处理</a>
			</dd>
		
			<dd>
				<span class="channel-num">11</span>
				<a href="http://c.biancheng.net/java/110/">Java集合、泛型和枚举</a>
			</dd>
		
			<dd>
				<span class="channel-num">12</span>
				<a href="http://c.biancheng.net/java/120/">Java反射机制</a>
			</dd>
		
			<dd>
				<span class="channel-num">13</span>
				<a href="http://c.biancheng.net/java/130/">Java输入/输出流</a>
			</dd>
		<dd class="this"> <span class="channel-num">14</span> <a href="http://c.biancheng.net/java/java/135/">Java注解</a> </dd><dl class="dl-sub"><dd>14.1 <a href="http://c.biancheng.net/view/6992.html">Java注解概念及作用</a></dd><dd>14.2 <a href="http://c.biancheng.net/view/6993.html">Java @Override注解</a></dd><dd>14.3 <a href="http://c.biancheng.net/view/6995.html">Java @Deprecated注解</a></dd><dd>14.4 <a href="http://c.biancheng.net/view/7002.html">Java @SuppressWarnings注解</a></dd><dd>14.5 <a href="http://c.biancheng.net/view/7003.html">Java @SafeVarargs注解</a></dd><dd>14.6 <a href="http://c.biancheng.net/view/7013.html">Java @FunctionalInterface注解</a></dd><dd>14.7 <a href="http://c.biancheng.net/view/7009.html">Java元注解作用及使用</a></dd><dd>14.8 <a href="http://c.biancheng.net/view/7016.html">Java自定义注解</a></dd><dd class="active">14.9 <a href="http://c.biancheng.net/view/vip_7084.html">Java通过反射获取注解信息</a><span class="glyphicon glyphicon-usd"></span></dd></dl>
	</div>
</div>
	<div id="article-wrap">
		<div id="article">
			<div class="arc-info">
	<span class="position"><span class="glyphicon glyphicon-map-marker"></span> <a href="http://c.biancheng.net/">首页</a> &gt; <a href="http://c.biancheng.net/java/">Java教程</a> &gt; <a href="http://c.biancheng.net/java/java/135/">Java注解</a></span>
	<span class="read-num">阅读：6,097</span>
</div>

<div id="ad-position-bottom"></div>
			<h1>Java通过反射获取注解信息</h1>
			<div class="pre-next-page clearfix">
                    <span class="pre left"><span class="icon">&lt;</span> <span class="text-brief text-brief-pre">上一页</span><a href="http://c.biancheng.net/view/7016.html">Java自定义注解</a></span>
                    <span class="next right"><a href="http://c.biancheng.net/view/vip_7084.html">Java通过反射获取注解信息</a><span class="text-brief text-brief-next">下一页</span> <span class="icon">&gt;</span></span>
                </div>
			<div id="ad-arc-top"><p class="pic"></p><p class="text" adid="java-1v1q2a"><a href="http://c.biancheng.net/view/7552.html" target="_blank" style="color: #D33428;">C语言中文网推出辅导班啦，包括<span style="color: #008000;">「C语言辅导班、C++辅导班、算法/数据结构辅导班」</span>，全部都是一对一教学：一对一辅导 + 一对一答疑 + 布置作业 + 项目实践 + 永久学习。QQ在线，随时响应！</a></p></div>
			<div id="arc-body">使用注解修饰了类、方法、变量等成员之后，这些注解不会自己生效，必须由开发者提供相应的工具来提取处理。要想获取类、方法或变量的注解信息，必须通过 Java 的反射技术来获取 Annotation 对象，除此之外没有其它方法。<br>
<br>
下面主要介绍 Java 基于反射机制获取注解的方法，并以结合实例的方式讲解了获取注解信息的具体实现方法与操作事项。<br>
<br>
所有 Java 注解类型都继承于 java.lang.annotation.Annotation 接口，该接口代表程序中可以接收注解的程序元素。该接口主要有如下几个实现类。
<ol>
	<li>
		Class：类定义。</li>
	<li>
		Constructor：构造方法定义。</li>
	<li>
		Field：类的成员变量定义。</li>
	<li>
		Method：类的方法定义。</li>
	<li>
		Package：类的包定义。</li>
</ol>
<br>
java.lang.reflect 包下主要包含一些实现反射功能的工具类，从 Java 5 开始，java.lang.reflect 包所提供的反射 API 增加了读取运行时注解的能力。只有当定义注解时使用了 @Retention(RetentionPolicy.RUNTIME) 修饰，该注解才会在运行时可见。<br>
<br>
AnnotatedElement 接口是所有程序元素（如 Class、Method、Constructor 等）的父接口，所以程序通过反射获取了某个类的 AnnotatedElement 对象（如 Class、Method、 Constructor 等）之后，程序就可以调用该对象的如下几个方法来访问注解信息，方法名称及作用如下表所示。<br>
<br>
<table>
	<tbody>
		<tr>
			<th>
				方法名</th>
			<th>
				作用</th>
		</tr>
		<tr>
			<td>
				&lt;A extends Annotation&gt; A getAnnotation(Class&lt;A&gt; annotationClass)</td>
			<td>
				如果该元素存在 annotationClass 类型的注解，则返回注解，否则返回 null</td>
		</tr>
		<tr>
			<td>
				&lt;A extends Annotation&gt; A getDeclaredAnnotation(Class&lt;A&gt; annotationClass)</td>
			<td>
				这是 Java 8 新增的方法，该方法尝试获取直接修饰该程序元素、指定类型的注解。如果该类型的注解不存在，则返回 null</td>
		</tr>
		<tr>
			<td>
				Annotation[] getAnnotations()</td>
			<td>
				返回该元素上存在的所有注解</td>
		</tr>
		<tr>
			<td>
				Annotation[] getDeclaredAnnotations()</td>
			<td>
				返回直接存在于该元素的所有注解（和 getAnnotations() 的区别在于该方法将不返回继承的注释）</td>
		</tr>
		<tr>
			<td>
				boolean isAnnotationPresent(Class&lt;? extends Annotation&gt; annotationClass)</td>
			<td>
				判断该元素上是否存在 annotationClass 类型的注解，如果存在则返回 true，否则返回 false。</td>
		</tr>
		<tr>
			<td>
				&lt;A extends Annotation&gt; A[] getAnnotationsByType(Class&lt;A&gt; annotationClass)</td>
			<td>
				该方法与前面介绍的 getAnnotation() 方法基本相似。但由于 Java 8 增加了重复注解功能，因此需要使用该方法获取该元素存在 annotationClass 类型的多个注解。</td>
		</tr>
		<tr>
			<td>
				&lt;A extends Annotation&gt; A[] getDeclaredAnnotationsByType(Class&lt;A&gt; annotationClass)</td>
			<td>
				该方法与前面介绍的 getDeclaredAnnotations() 方法基本相似。但由于 Java 8 增加了重复注解功能，因此需要使用该方法获取该元素存在 annotationClass 类型的多个注解。</td>
		</tr>
	</tbody>
</table>
根据官方的命名规则，可以总结出以下几条：
<ol>
	<li>
		getDeclaredAnnotationXXXX：只可以获取直接存在的注解信息，即直接修饰在某个元素（类、属性和方法）上的注解。</li>
	<li>
		getXXXXByType：可以获取间接存在的注解信息</li>
	<li>
		getAnnotationXXX：可以获取继承的注解信息</li>
</ol>
<br>
注意：所有的方法都可以获取直接注解信息
<blockquote>
	<p>
		为了获得程序中的元素（如类、方法等），必须使用反射知识，如果不了解 Java 反射，可先阅读学习《<a href="http://c.biancheng.net/view/6907.html" target="_blank">Java反射机制是什么？Java反射机制的基本概念</a>》一节。</p>
</blockquote>
<h4>
	例​&nbsp;1&nbsp;</h4>
在 SpringMVC 中，经常使用 <code>@RequestMapping(value="")</code> 注解，这样 Spring 就会将我们填写的 value 值当作路径存放在 map 结构中让我们访问。下面我们使用 getAnnotation() 方法来实现这个功能，代码如下。
<div class="snippet-container" style="undefined;"><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style="display:none;"><pre><a class="snippet-copy sh_url" href="http://c.biancheng.net/view/vip_7084.html#" style="display: none;">复制</a><a class="snippet-text sh_url" href="http://c.biancheng.net/view/vip_7084.html#">纯文本</a><a class="snippet-window sh_url" href="http://c.biancheng.net/view/vip_7084.html#">复制</a></pre></div><pre class="java sh_java snippet-formatted sh_sourceCode"><ol class="snippet-num"><li><span class="sh_comment">/**</span></li><li><span class="sh_comment">* 这是自定义注解的类</span></li><li><span class="sh_comment">*/</span></li><li>@<span class="sh_function">Target</span><span class="sh_symbol">(</span><span class="sh_cbracket">{</span> ElementType<span class="sh_symbol">.</span>TYPE<span class="sh_symbol">,</span> ElementType<span class="sh_symbol">.</span>METHOD <span class="sh_cbracket">}</span><span class="sh_symbol">)</span></li><li>@<span class="sh_function">Retention</span><span class="sh_symbol">(</span>RetentionPolicy<span class="sh_symbol">.</span>RUNTIME<span class="sh_symbol">)</span></li><li>@Documented</li><li><span class="sh_keyword">public</span> @<span class="sh_keyword">interface</span><span class="sh_normal"> </span><span class="sh_classname">MyRequestMapping</span> <span class="sh_cbracket">{</span></li><li>    <span class="sh_usertype">String</span><span class="sh_normal"> </span><span class="sh_function">value</span><span class="sh_symbol">();</span> <span class="sh_comment">// 这是注解的一个属性字段，也就是在使用注解时填写在括号里的参数</span></li><li><span class="sh_cbracket">}</span></li></ol></pre><pre class="snippet-textonly sh_sourceCode" style="display:none;">/**
* 这是自定义注解的类
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyRequestMapping {
    String value(); // 这是注解的一个属性字段，也就是在使用注解时填写在括号里的参数
}</pre></div></div>
创建 TestController 类，然后在 TestController 类中使用 @MyRequestMapping 注解，代码如下：
<div class="snippet-container" style="undefined;"><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style="display:none;"><pre><a class="snippet-copy sh_url" href="http://c.biancheng.net/view/vip_7084.html#" style="display: none;">复制</a><a class="snippet-text sh_url" href="http://c.biancheng.net/view/vip_7084.html#">纯文本</a><a class="snippet-window sh_url" href="http://c.biancheng.net/view/vip_7084.html#">复制</a></pre></div><pre class="java sh_java snippet-formatted sh_sourceCode"><ol class="snippet-num"><li>@<span class="sh_function">MyRequestMapping</span><span class="sh_symbol">(</span><span class="sh_string">"/test"</span><span class="sh_symbol">)</span></li><li><span class="sh_keyword">public</span> <span class="sh_keyword">class</span><span class="sh_normal"> </span><span class="sh_classname">TestController</span> <span class="sh_cbracket">{</span></li><li>    <span class="sh_keyword">public</span> <span class="sh_type">void</span> <span class="sh_function">test</span><span class="sh_symbol">()</span> <span class="sh_cbracket">{</span></li><li>        System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_string">"进入Test方法"</span><span class="sh_symbol">);</span></li><li>    <span class="sh_cbracket">}</span></li><li><span class="sh_cbracket">}</span></li></ol></pre><pre class="snippet-textonly sh_sourceCode" style="display:none;">@MyRequestMapping("/test")
public class TestController {
    public void test() {
        System.out.println("进入Test方法");
    }
}</pre></div></div>
测试类代码如下：
<div class="snippet-container" style="undefined;"><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style="display:none;"><pre><a class="snippet-copy sh_url" href="http://c.biancheng.net/view/vip_7084.html#" style="display: none;">复制</a><a class="snippet-text sh_url" href="http://c.biancheng.net/view/vip_7084.html#">纯文本</a><a class="snippet-window sh_url" href="http://c.biancheng.net/view/vip_7084.html#">复制</a></pre></div><pre class="java sh_java snippet-formatted sh_sourceCode"><ol class="snippet-num"><li><span class="sh_keyword">public</span> <span class="sh_keyword">class</span><span class="sh_normal"> </span><span class="sh_classname">Test</span> <span class="sh_cbracket">{</span></li><li>    <span class="sh_keyword">public</span> <span class="sh_keyword">static</span> <span class="sh_type">void</span> <span class="sh_function">main</span><span class="sh_symbol">(</span>String<span class="sh_symbol">[]</span> args<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>        <span class="sh_usertype">Class&lt;?&gt;</span><span class="sh_normal"> </span>c <span class="sh_symbol">=</span> TestController<span class="sh_symbol">.</span><span class="sh_keyword">class</span><span class="sh_symbol">;</span></li><li>        <span class="sh_usertype">MyRequestMapping</span><span class="sh_normal"> </span>baseRequestMapping <span class="sh_symbol">=</span> c<span class="sh_symbol">.</span><span class="sh_function">getAnnotation</span><span class="sh_symbol">(</span>MyRequestMapping<span class="sh_symbol">.</span><span class="sh_keyword">class</span><span class="sh_symbol">);</span></li><li>        System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span>baseRequestMapping<span class="sh_symbol">.</span><span class="sh_function">value</span><span class="sh_symbol">());</span> <span class="sh_comment">// 输出value的值</span></li><li>    <span class="sh_cbracket">}</span></li><li><span class="sh_cbracket">}</span></li></ol></pre><pre class="snippet-textonly sh_sourceCode" style="display:none;">public class Test {
    public static void main(String[] args) {
        Class&lt;?&gt; c = TestController.class;
        MyRequestMapping baseRequestMapping = c.getAnnotation(MyRequestMapping.class);
        System.out.println(baseRequestMapping.value()); // 输出value的值
    }
}</pre></div></div>
输出结果为<code>/test</code>。<br>
<br>
注意：Class&lt;?&gt; 中的 &lt;?&gt; 是必须写的，你可以把<code>?</code>改成要限定的类型，但是必须要写这个泛型限定才行，否则会编译错误。
<h4>
	例 2</h4>
下面我们使用 getAnnotations() 方法获取多个注解，并输出。<br>
<br>
1）自定义 Person 注解，代码如下：
<div class="snippet-container" style="undefined;"><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style="display:none;"><pre><a class="snippet-copy sh_url" href="http://c.biancheng.net/view/vip_7084.html#" style="display: none;">复制</a><a class="snippet-text sh_url" href="http://c.biancheng.net/view/vip_7084.html#">纯文本</a><a class="snippet-window sh_url" href="http://c.biancheng.net/view/vip_7084.html#">复制</a></pre></div><pre class="java sh_java snippet-formatted sh_sourceCode"><ol class="snippet-num"><li>@<span class="sh_function">Target</span><span class="sh_symbol">(</span><span class="sh_cbracket">{</span> ElementType<span class="sh_symbol">.</span>TYPE <span class="sh_cbracket">}</span><span class="sh_symbol">)</span></li><li>@Inherited</li><li>@<span class="sh_function">Retention</span><span class="sh_symbol">(</span>RetentionPolicy<span class="sh_symbol">.</span>RUNTIME<span class="sh_symbol">)</span></li><li><span class="sh_keyword">public</span> @<span class="sh_keyword">interface</span><span class="sh_normal"> </span><span class="sh_classname">Person</span> <span class="sh_cbracket">{</span></li><li>    <span class="sh_usertype">String</span><span class="sh_normal"> </span><span class="sh_function">value</span><span class="sh_symbol">();</span></li><li><span class="sh_cbracket">}</span></li></ol></pre><pre class="snippet-textonly sh_sourceCode" style="display:none;">@Target({ ElementType.TYPE })
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface Person {
    String value();
}</pre></div></div>
2）修改例 1 中的 TestController 类，代码如下：
<div class="snippet-container" style="undefined;"><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style="display:none;"><pre><a class="snippet-copy sh_url" href="http://c.biancheng.net/view/vip_7084.html#" style="display: none;">复制</a><a class="snippet-text sh_url" href="http://c.biancheng.net/view/vip_7084.html#">纯文本</a><a class="snippet-window sh_url" href="http://c.biancheng.net/view/vip_7084.html#">复制</a></pre></div><pre class="java sh_java snippet-formatted sh_sourceCode"><ol class="snippet-num"><li>@<span class="sh_function">MyRequestMapping</span><span class="sh_symbol">(</span><span class="sh_string">"/test"</span><span class="sh_symbol">)</span></li><li>@<span class="sh_function">Person</span><span class="sh_symbol">(</span><span class="sh_string">"C"</span><span class="sh_symbol">)</span></li><li><span class="sh_keyword">public</span> <span class="sh_keyword">class</span><span class="sh_normal"> </span><span class="sh_classname">TestController</span> <span class="sh_cbracket">{</span></li><li>    <span class="sh_keyword">public</span> <span class="sh_type">void</span> <span class="sh_function">test</span><span class="sh_symbol">()</span> <span class="sh_cbracket">{</span></li><li>        System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_string">"进入Test方法"</span><span class="sh_symbol">);</span></li><li>    <span class="sh_cbracket">}</span></li><li><span class="sh_cbracket">}</span></li></ol></pre><pre class="snippet-textonly sh_sourceCode" style="display:none;">@MyRequestMapping("/test")
@Person("C")
public class TestController {
    public void test() {
        System.out.println("进入Test方法");
    }
}</pre></div></div>
3）创建测试类，获取 TestController 类里的所有注解，并将这些注解打印出来。代码如下所示：
<div class="snippet-container" style="undefined;"><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style="display:none;"><pre><a class="snippet-copy sh_url" href="http://c.biancheng.net/view/vip_7084.html#" style="display: none;">复制</a><a class="snippet-text sh_url" href="http://c.biancheng.net/view/vip_7084.html#">纯文本</a><a class="snippet-window sh_url" href="http://c.biancheng.net/view/vip_7084.html#">复制</a></pre></div><pre class="java sh_java snippet-formatted sh_sourceCode"><ol class="snippet-num"><li><span class="sh_keyword">public</span> <span class="sh_keyword">class</span><span class="sh_normal"> </span><span class="sh_classname">Test</span> <span class="sh_cbracket">{</span></li><li>    <span class="sh_keyword">public</span> <span class="sh_keyword">static</span> <span class="sh_type">void</span> <span class="sh_function">main</span><span class="sh_symbol">(</span>String<span class="sh_symbol">[]</span> args<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>        <span class="sh_usertype">Class&lt;?&gt;</span><span class="sh_normal"> </span>c <span class="sh_symbol">=</span> TestController<span class="sh_symbol">.</span><span class="sh_keyword">class</span><span class="sh_symbol">;</span></li><li>        Annotation<span class="sh_symbol">[]</span> atnsArray <span class="sh_symbol">=</span> c<span class="sh_symbol">.</span><span class="sh_function">getAnnotations</span><span class="sh_symbol">();</span></li><li>        <span class="sh_keyword">for</span> <span class="sh_symbol">(</span><span class="sh_usertype">Annotation</span><span class="sh_normal"> </span>an <span class="sh_symbol">:</span> atnsArray<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>            System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span>an<span class="sh_symbol">);</span></li><li>        <span class="sh_cbracket">}</span></li><li>    <span class="sh_cbracket">}</span></li><li><span class="sh_cbracket">}</span></li></ol></pre><pre class="snippet-textonly sh_sourceCode" style="display:none;">public class Test {
    public static void main(String[] args) {
        Class&lt;?&gt; c = TestController.class;
        Annotation[] atnsArray = c.getAnnotations();
        for (Annotation an : atnsArray) {
            System.out.println(an);
        }
    }
}</pre></div></div>
输出结果为：
<p class="info-box">
	@MyRequestMapping(value=/test)<br>
	@Person(value=C)</p>
</div>
			<div id="arc-append">
	<p>关注微信公众号「站长严长生」，在手机上阅读所有教程，随时随地都能学习。本公众号由<a class="col-link" href="http://c.biancheng.net/view/8092.html" target="_blank" rel="nofollow">C语言中文网站长</a>运营，每日更新，坚持原创，敢说真话，凡事有态度。</p>
	<p style="margin-top:12px; text-align:center;">
		<img width="180" src="./14.9Java通过反射获取注解信息_files/qrcode_weixueyuan_original.png" alt="魏雪原二维码"><br>
		<span class="col-green">微信扫描二维码关注公众号</span>
	</p>
</div>
<!-- <p>关注微信公众号「魏雪原」，跟着<a class="col-link" href="/view/8092.html" target="_blank">站长</a>一起学习，拒绝躺平，跳出内卷，用知识完成阶级跃升：</p>
	<ul>
		<li>每日更新，坚持原创，敢说真话，凡事有态度。</li>
		<li>编程+求职+创业+营销，既有硬知识，也有软技能。</li>
		<li>手机端阅读教程，随时随地都能学习。</li>
		<li>即将绑定网站部分功能，包括注册登录、找回密码、获取下载链接等。</li>
	</ul> -->
			<div class="pre-next-page clearfix">
                    <span class="pre left"><span class="icon">&lt;</span> <span class="text-brief text-brief-pre">上一页</span><a href="http://c.biancheng.net/view/7016.html">Java自定义注解</a></span>
                    <span class="next right"><a href="http://c.biancheng.net/view/vip_7084.html">Java通过反射获取注解信息</a><span class="text-brief text-brief-next">下一页</span> <span class="icon">&gt;</span></span>
                </div>
			<div id="ad-arc-bottom"></div>

<!-- <div id="ad-bottom-weixin" class="clearfix">
	<div class="left" style="width: 535px;">
		<p><span class="col-red">编程帮</span>，一个分享编程知识的公众号。跟着<a class="col-link" href="/cpp/about/author/" target="_blank">站长</a>一起学习，每天都有进步。</p>
		<p>通俗易懂，深入浅出，一篇文章只讲一个知识点。</p>
		<p>文章不深奥，不需要钻研，在公交、在地铁、在厕所都可以阅读，随时随地涨姿势。</p>
		<p>文章不涉及代码，不烧脑细胞，人人都可以学习。</p>
		<p>当你决定关注「编程帮」，你已然超越了90%的程序员！</p>
	</div>
	<div class="right" style="width: 150px;">
		<img width="150" src="/templets/new/images/erweima_biancheng.gif?v=1.6.72" alt="编程帮二维码" /><br />
		<span class="col-green">微信扫描二维码关注</span>
	</div>
</div> -->

<div id="nice-arcs" class="box-bottom">
	<h4>优秀文章</h4>
	<ul class="clearfix">
<li><a href="http://c.biancheng.net/view/602.html" title="C++ copy_if(STL copy_if)算法详解">C++ copy_if(STL copy_if)算法详解</a></li>
<li><a href="http://c.biancheng.net/mysql/addtime.html" title="MySQL ADDTIME函数：时间加法运算">MySQL ADDTIME函数：时间加法运算</a></li>
<li><a href="http://c.biancheng.net/view/3162.html" title="Docker Dockerfile简介">Docker Dockerfile简介</a></li>
<li><a href="http://c.biancheng.net/view/3872.html" title="与云计算相关的开源软件有哪些？">与云计算相关的开源软件有哪些？</a></li>
<li><a href="http://c.biancheng.net/view/4244.html" title="Spring目录结构和基础JAR包介绍">Spring目录结构和基础JAR包介绍</a></li>
<li><a href="http://c.biancheng.net/view/5019.html" title="JSP JSTL &lt;sql:transaction&gt;标签：事务处理">JSP JSTL &lt;sql:transaction&gt;标签：事务处理</a></li>
<li><a href="http://c.biancheng.net/view/6847.html" title="Java Set集合：HashSet和TreeSet类">Java Set集合：HashSet和TreeSet类</a></li>
<li><a href="http://c.biancheng.net/view/7755.html" title="Python保留字（关键字）一览表">Python保留字（关键字）一览表</a></li>
<li><a href="http://c.biancheng.net/view/8077.html" title="MinGW下载和安装教程">MinGW下载和安装教程</a></li>
<li><a href="http://c.biancheng.net/view/vip_8325.html" title="MySql Command Line Client闪退原因及解决方案">MySql Command Line Client闪退原因及解决方案</a></li>
</ul>
</div>
		</div>
		
	</div>
</div>
<script type="text/javascript">
// 当前文章ID
window.arcIdRaw = 'a_' + 7084;
window.arcId = "3c72/XcJrXM4DlHoIbTTQxa5BkjldK1/KiQLZ/YONwRrIjzu9F7vfyz8ACE";
window.typeidChain = "335|79";
</script>
<div id="footer" class="clearfix">
	<div class="info left">
	<p>精美而实用的网站，分享优质编程教程，帮助有志青年。千锤百炼，只为大作；精益求精，处处斟酌；这种教程，看一眼就倾心。</p>
	<p>
		<a href="http://c.biancheng.net/view/8066.html" target="_blank" rel="nofollow">关于网站</a> <span>|</span>
		<a href="http://c.biancheng.net/view/8092.html" target="_blank" rel="nofollow">关于站长</a> <span>|</span>
		<a href="http://c.biancheng.net/view/8097.html" target="_blank" rel="nofollow">如何完成一部教程</a> <span>|</span>
		<a href="http://c.biancheng.net/view/8093.html" target="_blank" rel="nofollow">联系我们</a> <span>|</span>
		<a href="http://c.biancheng.net/sitemap/" target="_blank" rel="nofollow">网站地图</a>
	</p>
	<p>Copyright ©2012-2021 biancheng.net, <a href="http://www.beian.miit.gov.cn/" target="_blank" rel="nofollow" style="color:#666;">陕ICP备15000209号</a></p>
	</div>
	<img class="right" src="./14.9Java通过反射获取注解信息_files/logo_bottom.gif" alt="底部Logo">
	<span id="return-top"><b>↑</b></span>
</div>

<script type="text/javascript">
window.siteId = 4;
window.cmsTemplets = "/templets/new";
window.cmsTempletsVer = "1.6.72";
</script>

<script src="./14.9Java通过反射获取注解信息_files/jquery1.12.4.min.js.下载"></script>
<script src="./14.9Java通过反射获取注解信息_files/common.js.下载"></script>
<span style="display:none;"><script src="./14.9Java通过反射获取注解信息_files/z_stat.php" type="text/javascript" defer="defer" async="async"></script></span>

</body></html>